package com.pac.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.pac.API;
import com.pac.CustomView.GridViewToScrollView;
import com.pac.MyApp;
import com.pac.R;
import com.pac.bean.course.CourseChildItem;
import com.pac.bean.course.CourseItem;
import com.pac.utils.ImageUtils;

import java.util.List;

import se.emilsjolander.stickylistheaders.ExpandableStickyListHeadersListView;
import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;
import se.emilsjolander.stickylistheaders.StickyListHeadersListView;

/**
 * fragment格式样板
 * Created by 兴盛 on 2015/4/22.
 */
public class FMCourse extends Fragment {

    private ExpandableStickyListHeadersListView mExpandableList;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fm_course, container, false);
        mExpandableList = (ExpandableStickyListHeadersListView) view.findViewById(R.id.list);
        MyApp.httpUtils.send(HttpRequest.HttpMethod.GET, API.COURSE, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> objectResponseInfo) {
                List<CourseItem> courseItemList = CourseItem.getListFromJsonStr(objectResponseInfo.result);
                mExpandableList.setAdapter(new LvAdapter(courseItemList));
                //默认合并
                for (int i = 0; i < mExpandableList.getCount(); i++) {
                    mExpandableList.collapse(i);
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
        //设置展开合并事件
        mExpandableList.setOnHeaderClickListener(new StickyListHeadersListView.OnHeaderClickListener() {
            @Override
            public void onHeaderClick(StickyListHeadersListView l, View header, int itemPosition, long headerId, boolean currentlySticky) {
                if (mExpandableList.isHeaderCollapsed(headerId)) {
                    mExpandableList.expand(headerId);
                    header.setBackgroundResource(R.color.course_header_expand);
                } else {
                    mExpandableList.collapse(headerId);
                    header.setBackgroundResource(R.color.course_header_collapse);
                }
            }
        });
        return view;
    }

    class LvAdapter extends BaseAdapter implements StickyListHeadersAdapter {
        private List<CourseItem> courses;

        LvAdapter(List<CourseItem> courses) {
            this.courses = courses;
        }

        @Override
        public View getHeaderView(int position, View convertView, ViewGroup parent) {
            //设置HeaderView
            HeaderHolder holder;
            if (convertView == null) {
                holder = new HeaderHolder();
                convertView = getActivity().getLayoutInflater().inflate(R.layout.fm_course_header, null);
                holder.img = (ImageView) convertView.findViewById(R.id.img);
                holder.name = (TextView) convertView.findViewById(R.id.txt_name);
                holder.children = (TextView) convertView.findViewById(R.id.txt_children);
                convertView.setTag(holder);
            } else {
                holder = (HeaderHolder) convertView.getTag();
            }
            ImageUtils.loadImg(getActivity(), holder.img, courses.get(position).getIco());
            holder.name.setText(courses.get(position).getName());
            holder.children.setText(courses.get(position).getChildrenNames());
            return convertView;
        }

        @Override
        public long getHeaderId(int position) {
            return position;
        }

        @Override
        public int getCount() {
            return courses.size();
        }

        @Override
        public Object getItem(int position) {
            return courses.get(position).getChild();
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = getActivity().getLayoutInflater().inflate(R.layout.fm_course_child, null);
            GridViewToScrollView gridView = (GridViewToScrollView) view.findViewById(R.id.gv);
            gridView.setAdapter(new GvAdapter(courses.get(position).getChild()));
            return view;
        }
    }

    class HeaderHolder {
        ImageView img;
        TextView name;
        TextView children;
    }

    class GvAdapter extends BaseAdapter {
        private List<CourseChildItem> list;

        GvAdapter(List<CourseChildItem> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View view = getActivity().getLayoutInflater().inflate(R.layout.fm_course_child_item, null);
            TextView textView = (TextView) view.findViewById(R.id.txt);
            textView.setText(list.get(position).getName());
            return view;
        }
    }
}
